//
//  Update.m
//  TLock
//
//  Created by lenny on 2017/11/2.
//  Copyright © 2017年 aliyun. All rights reserved.
//

#import "Update.h"
#define appShopId @"1327801074"
#define findShopVersion [@"https://itunes.apple.com/lookup?id=" stringByAppendingString:appShopId]
#define goToAppShop [[@"https://itunes.apple.com/cn/app/alock/id" stringByAppendingString:appShopId] stringByAppendingString:@"?mt=8"]

@implementation Update

-(void)exec:(id)app{
    //   1308830129 | 1297186522
    @try{
        
        NSURLSession *session = [NSURLSession sharedSession];

        NSURL *url = [NSURL URLWithString:findShopVersion] ;

        NSURLSessionTask *task = [session dataTaskWithURL:url
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                            
//                                            NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
                                            @try {
                                                NSError *errorJs;
                                                id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&errorJs];
                                                NSDictionary *appInfo = (NSDictionary *)jsonObject;
                                                NSLog(@"--------appinfo%@",appInfo);
                                                NSArray *infoContent = [appInfo objectForKey:@"results"];
                                                
                                                
                                                NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
                                                
                                                NSLog(@"商店的版本是 %@",version);
                                                
                                                NSString *skipUrl = [[infoContent objectAtIndex:0] objectForKey:@"trackViewUrl"];
                                                
                                                NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
                                                NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
                                                NSLog(@"当前的版本是 %@",currentVersion);
                                                
                                                if (![version isEqualToString:currentVersion]) {
                                                    NSLog(@"%@=====%@==当前",version,currentVersion);
                                                    [self alertHandler:app];
                                                }
                                            } @catch (NSException *exception) {
                                                return ;
                                            } @finally {
                                                
                                            }
                                            
                                        }];
        // 启动任务
        [task resume];
    }
    @catch(NSException *exception){
        return;
    }@finally {
        
    }
    
    
}

-(void)alertHandler:(id)app{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"发现新本需要更新"
                                                                   message:@"温馨提示"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              NSURL *url = [NSURL URLWithString:goToAppShop];
                                                              [[UIApplication sharedApplication] openURL:url];
                                                          }];
    UIAlertAction *cannelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             NSLog(@"noooono");
                                                         }];
    
    [alert addAction:defaultAction];
    [alert addAction:cannelAction];
    [app presentViewController:alert animated:YES completion:nil];
}
@end
